package com.example.demo.controller;

import com.example.demo.util.ZTApiSDKUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @program: demo
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2022-04-20 15:42
 **/
@RestController()
public class TestController {

    @PostMapping("/test/{sleep}/{body}")
    public List<Map<String,Object>> req(@PathVariable Integer sleep,
                                        @PathVariable Integer body,
                                        @RequestBody Map<String,Object> map) throws InterruptedException {
        if(sleep>0){
            Thread.sleep(sleep);
        }
        if(body<=0){
            Map<String,Object> map2 = new HashMap<>();
            for (Map.Entry<String,Object> entry:map.entrySet()) {
                if(body==0){
                    break;
                }
                map2.put(entry.getKey(),entry.getValue());
                body++;
            }
            return Arrays.asList(map2);
        }
        if(body==1){
            return Arrays.asList(map);
        }
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0;i < body; i++){
            list.add(map);
        }
        return list;
    }
    // 应用id
    static final String APP_ID = "test";
    // 应用秘钥
    static final String APP_SECRET = "test";
    // {"grant_type":"client_credentials","client_id":"test","client_secret":"test"}
    static final String GET_TOKEN_URL = "https://10.100.20.13:8443/test/oauth2/token";
    static final String REQ_URL = "https://10.100.20.13:8443/test?id=123";
    // http://192.168.122.46:10010/test/sign

//    http://10.100.20.12:8001/clustering/data-planes
    @Value("${key}")
    public String key;

    @GetMapping("/test/sign")
    public Object sign() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String token = ZTApiSDKUtil.getToken();
        Map<String, String> map = ZTApiSDKUtil.httpGetClientHeaders(REQ_URL, token);
        map.put("key",key);
        return map;
    }

}
